package org.wyn.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.bson.types.ObjectId;
import org.wyn.mongodb.bean.Album;
import org.wyn.mongodb.bean.Picture;
import org.wyn.mongodb.common.ConnectionPool;
import org.wyn.mongodb.dao.impl.AlbumDao;
import org.wyn.mongodb.dao.impl.FreshThingDao;
import org.wyn.mongodb.dao.impl.UserDetailDao;
import org.wyn.service.IAlbumService;

import com.google.inject.Inject;
import com.mongodb.DB;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSInputFile;

public class AlbumServiceImpl implements IAlbumService{

	@Override
	public boolean createAlbum(String userName, String albumName) {
		return albumDao.createAlbum(userName, albumName);
	}
	@Override
	public List<Album> searchAlbum(String authorUserName) {
		return albumDao.createQuery().filter("userName", authorUserName).order("createTime").asList();
	}
	@Override
	public Album findAlbumById(String albumId) {
		return albumDao.findOne("id", new ObjectId(albumId));
	}
	@Override
	public boolean uploadImage(String albumId, File image, String contentType,
			String imageName) {
		Album a = albumDao.findOne("id",new ObjectId(albumId));
		if(null == a)//相册不存在
			return false;
		//图片存入GridFS
		GridFSInputFile gfsif = saveFileToGridFS(image, contentType, imageName);
		ObjectId picId = (ObjectId)gfsif.getId();
		Picture p = new Picture(picId,imageName);
		//照片加入相册中,相册中其实存的是图片ID,真正的图片放在GridFS中了
		a.getPictures().add(p);
		albumDao.save(a);
		albumDao.ensureIndexes();
		//保存新鲜事
		List<ObjectId> imageIds = new ArrayList<ObjectId>();
		imageIds.add(picId);
		freshThingDao.saveThingUploadImage(albumDao.getUserName(new ObjectId(albumId)), new ObjectId(albumId), imageIds);
		return true;
	}
	
	@Override
	public boolean uploadPortrait(String userName, File image,String contentType, String imageName) {
		//图片存入GridFS
		GridFSInputFile gfsif = saveFileToGridFS(image, contentType, imageName);
		ObjectId imageId = (ObjectId)gfsif.getId();
		//头像相册中添加图片
		albumDao.addImageToPortrait(userName, imageId);
		//user_detail中添加图片
		userDetailDao.changePortrait(userName, imageId);
		//保存新鲜事--上传头像
		freshThingDao.saveThingModifyPortrait(userName, imageId);
		return true;
	}
	/**
	 * 向GridFS中保存文件
	 * @param file 文件
	 * @param contentType 文件类型
	 * @param fileName 文件名
	 * @return 保存成功返回保存的GridFSInputFile对象实例,失败null
	 */
	private GridFSInputFile saveFileToGridFS(File file,String contentType,String fileName){
		DB db = ConnectionPool.getDB();
		GridFS gFs = new GridFS(db);
		GridFSInputFile gfsif = null;
		try {
			gfsif = gFs.createFile(file);
			gfsif.setContentType(contentType);
			gfsif.setFilename(fileName);
			gfsif.save();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return gfsif;
	}
	@Inject
	private AlbumDao albumDao;
	@Inject
	private FreshThingDao freshThingDao;
	@Inject
	private UserDetailDao userDetailDao;

	public FreshThingDao getFreshThingDao() {
		return freshThingDao;
	}
	public void setFreshThingDao(FreshThingDao freshThingDao) {
		this.freshThingDao = freshThingDao;
	}
	public AlbumDao getAlbumDao() {
		return albumDao;
	}
	public void setAlbumDao(AlbumDao albumDao) {
		this.albumDao = albumDao;
	}
	public UserDetailDao getUserDetailDao() {
		return userDetailDao;
	}
	public void setUserDetailDao(UserDetailDao userDetailDao) {
		this.userDetailDao = userDetailDao;
	}
}
